import {
	myShowLoading,
	myHideLoading,
} from "../utils/showToast.js";
import {
	Base64
} from "../utils/base64text.js";
async function textCheck(text) {
	myShowLoading("文本检测中");
	let encodeStr = Base64.encode(text);
	let c1 = new wx.cloud.Cloud({
		// 资源方 AppID
		resourceAppid: "wxce1468e05a9fe673",
		// 资源方环境 ID
		resourceEnv: "xzy-8gkjj1544349b4cb",
	});
	await c1.init();
	return new Promise((resolve, reject) => {
		c1.callFunction({
			name: "checkText",
			data: {
				text: encodeStr,
			},
		}).then((res) => {
			myHideLoading();
			if (res.result.Keywords.length == 0) {
				let detectionData = {
					isItPass: true,
				};
				resolve(detectionData);
				wx.showToast({
					title: "未检测到敏感词汇",
					icon: "none",
					duration: 2000,
				});
			} else {
				let detectionData = {
					isItPass: false,
					data: res.result.Keywords,
				};
				resolve(detectionData);
			}
		});
	});
}

async function wxTextDetection(text, showModal) {
	if (!showModal) {
		myShowLoading("文本检测中");
	}
	let c1 = new wx.cloud.Cloud({
		// 资源方 AppID
		resourceAppid: "wx7aa29052a47a142f",
		// 资源方环境 ID
		resourceEnv: "env-8gl599np37fe7786",
	});
	await c1.init();
	return new Promise((resolve, reject) => {
		c1.callFunction({
			name: "msgSC",
			data: {
				text: text,
			},
		}).then((res) => {
			console.log(res, "检测");
			myHideLoading();
			if (res.result.code == 200) {
				let detectionData = {
					isItPass: true,
				};
				resolve(detectionData);
			}else if(res.result.code == 500){
				wx.showToast({
					title:res.result.msg,
					icon:'none'
				})
			}else {
				let detectionData = {
					isItPass: false,
				};
				resolve(detectionData);
				if (!showModal) {
					wx.showModal({
						title: "提示",
						content: "检测到敏感关键词或文本为空，请修改后重试！",
						showCancel: false,
						success(res) {},
					});
				}
			}
		});
	});
}

export {
	textCheck,
	wxTextDetection
};